<template>
  <div class="login-container">
    <el-alert v-show="false" title="后台管理" type="success" :closable="false" style="position: fixed"></el-alert>
    <el-row>
      <el-col :xs="24" :sm="24" :md="12" :lg="14" :xl="14">
        <div style="color: transparent">左侧区域占位符</div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="10" :xl="10">
        <el-form status-icon class="login-form">
          <div class="form-header">
            <div class="title">hello !</div>
            <div class="title-tips">
              欢迎来到后台管理！
            </div>
          </div>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" autocomplete="off" v-model="loginForm.username" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" :prefix-icon="Lock" autocomplete="off" v-model="loginForm.password" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item prop="captchaCode">
            <el-row>
              <el-col :span="15">
                <el-input autocomplete="off" placeholder="请输入验证码" v-model="loginForm.captchaCode" />
              </el-col>
              <el-col :span="8" :offset="1">
                <el-image fit="contain" :src="image" @click="paresCaptcha" style="height: 100%; background: white" class="pointer" />
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-button class="login-btn" type="primary" size="large" @click="parseLogin">
              登陆
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'

// 导入依赖
import {ref,onMounted} from 'vue'
import {CaptchaType,getCaptcha,LoginType} from "@/api/login/index";
import userStore from '@/store/modules/login/userStore';
import { ElNotification } from 'element-plus';
import { useRouter } from 'vue-router';
//准备响应式数据
let image=ref("");

// 定义获取验证码方法，
  // 调用验证码方法，
  // 解析结果
let paresCaptcha=async ()=>{
    let {code,data}=await getCaptcha();
    if(code==200){
      image.value=data.image;
      //设置验证码的key
      loginForm.value.captchaKey=data.key;
    }
};
/**
 * 定义登录表单，指定类型LoginType
 */
let loginForm=ref<LoginType>({
  username:"",
  password:"",
  captchaKey:"",
  captchaCode:""
})
/**
 * 登录api,
    * 逻辑：
      * 定义登录api，在userStore,
      * 参数：四个表单，有captchaKey来自验证码api，自定义LoginType ,在api/login/,
      * 执行成功，存储token到pinia，存储到sessionstroage中，userStore内部完成
      * 之后再次请求，调用前置路由守卫，
      * 判定登录状态，跳转路由，useRouter,在vue-router
 */
let router=useRouter();
let parseLogin= async()=>{
    await userStore().doLogin(loginForm.value);
    // 提示登陆成功
    ElNotification({
      title:'登陆成功',
      message:'欢饮光临',
      type:'success',
    })
    //登陆成功，跳转路由
    router.push('/');
}

//初始化验证码
onMounted(()=>{
  paresCaptcha();
})

</script>

<style scoped lang="scss">
@import './index';
</style>
